﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Collections;
using TimKen.DataEntity;
using TimKen.DataAccess;
namespace TimKen.BussLogic
{
    /// <summary>
    /// 系统配置业务逻辑操作类
    /// </summary>
    public static class SystemConfigManager
    {
        private const string CreditCheck = "CreditCheck";
        private const string PasswordExpired = "PasswordExpired";
        private const string AmountToPointsR = "AmountToPointsR";
        private const string AmountToPointsD = "AmountToPointsD";
        private const string IsDisplayPromote = "IsDisplayPromote";

        private static Hashtable GetSystemConfig()
        {
            Hashtable ht = new Hashtable();
            SqlDataReader reader = DbHelperSQL.ExecuteReader("SELECT  ConfigCode,ConfigValue FROM SystemConfig");
            while (reader.Read())
            {
                ht.Add(reader[0].ToString(), reader[1].ToString());
            }
            reader.Close();
            reader.Dispose();
            return ht;
        }

        public static SystemConfig SystemConfig
        {
            get
            {
                SystemConfig config = new SystemConfig();
                Hashtable ht = GetSystemConfig();
                if (ht == null || ht.Count == 0)
                    return config;
                config.CreditCheck = Convert.ToBoolean(ht[CreditCheck].ToString());
                config.PasswordExpired = Convert.ToInt16(ht[PasswordExpired].ToString());
                config.AmountToPointsR = Convert.ToInt32(ht[AmountToPointsR].ToString());
                config.AmountToPointsD = Convert.ToInt32(ht[AmountToPointsD].ToString());
                config.IsDisplayPromote = Convert.ToBoolean(ht[IsDisplayPromote].ToString());
                return config;
            }
        }
        public static bool UpdateSystemConfig(SystemConfig config)
        {
            string sql = "Update  SystemConfig SET  ConfigValue='{0}' WHERE  ConfigCode='{1}'";
            sql = string.Format(sql, config.CreditCheck, CreditCheck);

            DataAccess.DbHelperSQL.ExecuteSql(sql);

            sql = "Update  SystemConfig SET  ConfigValue='{0}' WHERE  ConfigCode='{1}'";
            sql = string.Format(sql, config.PasswordExpired, PasswordExpired);
            DataAccess.DbHelperSQL.ExecuteSql(sql);


            sql = "Update  SystemConfig  SET ConfigValue='{0}' WHERE  ConfigCode='{1}'";
            sql = string.Format(sql, config.AmountToPointsR, AmountToPointsR);
            DataAccess.DbHelperSQL.ExecuteSql(sql);

            sql = "Update  SystemConfig  SET ConfigValue='{0}' WHERE  ConfigCode='{1}'";
            sql = string.Format(sql, config.AmountToPointsD, AmountToPointsD);
            DataAccess.DbHelperSQL.ExecuteSql(sql);

            sql = "Update  SystemConfig  SET ConfigValue='{0}' WHERE  ConfigCode='{1}'";
            sql = string.Format(sql, config.IsDisplayPromote, IsDisplayPromote);
            DataAccess.DbHelperSQL.ExecuteSql(sql);
            return true;
        }
    }
    public class SystemConfig
    {
        public bool CreditCheck = false;
        public int PasswordExpired = 0;
        public int AmountToPointsR = 0;
        public int AmountToPointsD = 0;
        public bool IsDisplayPromote = true;
    }
}
